package ca.usherbrooke.domus.sipnmove2.controller.phone;

import java.util.HashMap;
import java.util.Map;

import ca.usherbrooke.domus.sipnmove2.controller.ActionController;
import ca.usherbrooke.domus.sipnmove2.model.call.Call;
import ca.usherbrooke.domus.sipnmove2.model.call.CallFactory;
import ca.usherbrooke.domus.sipnmove2.model.phone.Phone;
import ca.usherbrooke.domus.sipnmove2.view.phone.PhoneView;

/**
 * Call action controller
 */
public class CallActionController implements ActionController {

	/**
	 * Phone to control
	 */
	private Phone phone;

	/**
	 * Phone view to get information from
	 */
	private PhoneView phoneView;

	/**
	 * CallId generator
	 */
	private CallIdGenerator callIdGenerator;

	/**
	 * Call factory
	 */
	private CallFactory callFactory;

	/**
	 * Do the controller action
	 */
	public void doAction() {
		String callId = callIdGenerator.generateCallId();

		// Create a new call from factory
		Call call = callFactory.createCall();
		phone.addCall(callId, call);

		// Configure the call parameters
		Map<String, Object> callParameters = new HashMap<String, Object>();
		callParameters.put("address", phoneView.getAddressTextField().getText());
		
		call.setParameters(callParameters);
		
		// Initiate the call
		call.initiate();
	}

	/**
	 * @param phone
	 *            the phone to set
	 */
	public void setPhone(Phone phone) {
		this.phone = phone;
	}

	/**
	 * @param phoneView
	 *            the phoneView to set
	 */
	public void setPhoneView(PhoneView phoneView) {
		this.phoneView = phoneView;
	}

	/**
	 * @param callIdGenerator
	 *            the callIdGenerator to set
	 */
	public void setCallIdGenerator(CallIdGenerator callIdGenerator) {
		this.callIdGenerator = callIdGenerator;
	}


	/**
	 * @param callFactory the callFactory to set
	 */
	public void setCallFactory(CallFactory callFactory) {
		this.callFactory = callFactory;
	}

}
